/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package engine.linkList;
import java.io.Serializable;
/**
 *
 * @author hjp
 */


//歌单集合
public class MusicLists extends AbstractList<MusicListNode> implements Serializable {
//    Node<MusicListNode> head;
//    Node<MusicListNode> tail;
//    int sum;

    MusicLists(){
        head = tail = null;
        sum = 0;
        isModify = false;
    }

//    @Override
//    void add(Node<MusicListNode> node) {
//        if (head == null){
//            head = tail = node;
//        }
//        else {
//            tail.addNext(node);
//            tail = node;
//        }
//    }

    /**
     * 删除整张链表
     */
    @Override
    void delete() {
        sum = 0;
        Node<MusicListNode> p = head;
        Node<MusicListNode> q;
        while (p != null){
            q = p.next;
            p.info.delete();
            p.delete();
            p = q;
        }
        head = tail = null;
        isModify = true;
    }


    /**
     * 从MusicLists中删除第index个歌单
     */
    @Override
    void delete(int index){
        Node<MusicListNode> p = head;
        if (sum == 1){
            head = tail = null;
        }
        else if (index == 1){
            head = head.next;
        }
        else if (index == sum){
            p = tail;
            tail = tail.pre;
        }
        else {
            index--;
            while (index != 0){
                p = p.next;
                --index;
            }
        }
        sum--;
        isModify = true;
        p.info.delete();
        p.delete();
    }

    /**
     * 在指定的MusicLists中按照指定的关键字搜索其中符合关键字的MusicList，
     * 然后将这些符合条件的MusicList合并成为一个新的MusicLists
     */
    MusicLists search(String key){
        MusicLists newLists = new MusicLists();
        Node<MusicListNode> pOld = head;
        Node<MusicListNode> newNode;
        while (pOld != null){
            if (pOld.info.isMatch(key)){
                newNode = new Node<>(pOld.info);
                newLists.addTail(newNode);
            }
            pOld = pOld.next;
        }
        newLists.isModify = false;
        return newLists;
    }
}
